package dateUtil;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @AUTHOR : yangfan
 * @DATE : 2017/1/12
 * @TIME : 17:33
 * @VERSION : 1.0
 * @DESC : 日期工具类
 */
public class DateUtil {

    private static final Date sysDate = new Date();

    private DateUtil() {
    }

    /**
     * Todo 格式化系统日期
     *
     * @param localFormat
     * @return date for string
     */
    public static String getSystemDateWithFormat(String localFormat) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(localFormat);
        return simpleDateFormat.format(sysDate);
    }

    /**
     * Todo 格式化特定日期以某种格式
     *
     * @param date
     * @param localFormat
     * @return format @param date with @param localFormat
     */
    public static String getDateWithFormat(Date date, String localFormat) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(localFormat);
        return simpleDateFormat.format(date);
    }

    /**
     * Todo 获取当前系统时间的当月天数
     *
     * @return
     */
    public static int getSystemDateDayNumberOfMonth() {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(sysDate);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * Todo 获取某个日期的当月天数
     *
     * @param date
     * @return int 月份天数
     */
    public static int getDayNumberOfMonth(Date date) {
        if (date == null) {
            //日期为null,返回系统时间的
            return getSystemDateDayNumberOfMonth();
        } else {
            //日期不为null，返回输入日期的
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        }
    }

    /**
     * Todo 获取当前系统日期为星期几
     *
     * @return int 星期数字
     */
    public static int getSystemDateWeek() {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(sysDate);
        return (calendar.get(Calendar.DAY_OF_WEEK) - 1) % 7;
    }

    /**
     * Todo 获取指定日期为星期几
     *
     * @param date
     * @return int 星期数字
     */
    public static int getDateWeek(Date date) {
        if (date == null) {
            return getSystemDateWeek();
        } else {
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            System.out.println();
            return (calendar.get(Calendar.DAY_OF_WEEK) - 1) % 7;
        }
    }

}
